<?php

/**
 * model_semester
 */

class Model_semester extends YOU {

	public	$semester_name = array(
	"1" => "第一学期",
	"2" => "第二学期",
	);
	public function __construct()
	{
		parent::__construct();
	}

	/** 读取成配置 **/
	public function getIni()
	{
		$semester_lists = $this->getAll();

		foreach ($semester_lists as $key => $value){
			$semester[$value['semester_id']] = $value['semester_name'];
		}

		return $semester;
	}

	/** 读取列表 **/
	public function getAll($field = "*")
	{
		$semester_lists = $this->db->fetchAll("SELECT {$field} FROM `tbl_semester` ");
		foreach ($semester_lists as $key => $value){
			$semester_lists[$key]['semester_name'] = $value['semester_xname'].$this->semester_name[$value['semester_name']];
		}

		return $semester_lists;
	}
	/** 读取学年列表 **/
	public function getAllRow($field = "*")
	{
		$semester_lists = $this->db->fetchAll("SELECT max(semester_xname),semester_xname,semester_id FROM `tbl_semester` ");
			
		foreach ($semester_lists as $key => $value){
			$semester_lists[$key]['semester_name'] = $value['semester_xname'].$this->semester_name[$value['semester_name']];
			$semester_lists[$key]['semester_id'] = $value['semester_id'];
		}

		return $semester_lists;
	}
	
	/** 读取名称根据id **/
	public function getNameById($semester_id)
	{
		$data = $this->getInfo($semester_id);

		return $data['semester_xname'].$this->semester_name[$data['semester_name']];
	}

	/** 得到数据 **/
	public function getInfo($semester_id, $field = "*")
	{
		$semester_data = $this->db->fetchRow("SELECT {$field} FROM `tbl_semester` WHERE semester_id='{$semester_id}'");
		if (!empty($semester_data)){
			$semester_data['semester_name'] = $semester_data['semester_xname'].$this->semester_name[$semester_data['semester_name']];
		}
		return $semester_data;
	}

	/** 得到最新一条 **/
	public function getNew($field = "*")
	{
		$date = date('Y-m-d',time());
		$semester_data = $this->db->fetchRow("SELECT {$field} FROM `tbl_semester` WHERE start_time <='{$date}' AND end_time >='{$date}'");
		if (!empty($semester_data)){
			$semester_data['semester_name'] = $semester_data['semester_xname'].$this->semester_name[$semester_data['semester_name']];
		}
		return $semester_data;
	}

	/** 删除学期 **/
	public function del($semester_id)
	{
		// 检查其他表是否已有本学期数据


		return $this->db->query("DELETE  FROM `tbl_semester` WHERE semester_id='{$semester_id}'");
	}

	/** 得到所有的学月 **/
	public function getMonthAll($semester_id)
	{
		$semester_lists = $this->db->fetchAll("SELECT semester_month FROM `tbl_week` WHERE semester_id='{$semester_id}'");

		foreach ($semester_lists as $key => $value){
			$month[$value['semester_month']]++;
		}

		return $month;

	}

	/** 得到学月开始和结束时间 **/
	public function getMonthTime($semester_id, $month)
	{	
		
		$semester_lists = $this->db->fetchAll("SELECT * FROM `tbl_week` WHERE semester_id='{$semester_id}' AND semester_month='{$month}' ORDER BY semester_month ASC");
		
		if (!empty($semester_lists)){
			foreach ($semester_lists as $key => $value){
				if ($key == 0)	{
					$start_time = $value['start_time'];
				}
			}
			$end_time  = $value['end_time'];

			$data = array(
			"start_time" => $start_time,
			"end_time"   => $end_time,
			);
			return $data;
		}else{
			return false;
		}

	}
	/** 得到学月开始和结束时间(zzl--这个好用) **/
	public function getMonthTimes($semester_id, $month)
	{	
		
		$month_lists = $this->db->fetchRow("SELECT * FROM `tbl_week` WHERE semester_id='{$semester_id}' AND semester_month='{$month}' ORDER BY start_time");
		
		if (!empty($month_lists)){
//		wq.12.7	  disease monthdetails
//			foreach ($month_lists as $key => $value){
//
//				if ($key == 0)	{
//					$start_time = $value['start_time'];
//
//				}
//			}
//			$end_time  = $value['end_time'];
            $start_time = $month_lists['start_time'];
            $month_lists = $this->db->fetchRow("SELECT * FROM `tbl_week` WHERE semester_id='{$semester_id}' AND semester_month='{$month}' ORDER BY end_time DESC ");
            $end_time = $month_lists['end_time'];
            $data = array(
			"start_time" => $start_time,
			"end_time"   => $end_time,
			);
			return $data;
		}else{
			return false;
		}

	}

	/** 根据时间得到周和学月 **/
	public function getWeekByTime($time)
	{
		
		$time = date("Y-m-d", strtotime($time));
		
		
		$data = $this->db->fetchRow("SELECT * FROM `tbl_week` WHERE start_time<='{$time}' AND end_time >= '{$time}' ");
		
		return $data;
		
	}
	/** 根据时间得到周和学月（奖励绩效工资表） **/
	public function getWeekByTimes($time)
	{		
		
		$data = $this->db->fetchRow("SELECT * FROM `tbl_week` WHERE start_time<='{$time}' AND end_time >= '{$time}' ");
		
		return $data;
		
	}

	/** 根据学期得到学月数 **/
	public function getMonthCount($semester_id)
	{
		$row = $this->db->fetchAll("SELECT semester_month  FROM `tbl_week` WHERE semester_id='{$semester_id}' GROUP BY semester_month");

		return count($row);
	}
	/** 根据学期、周得到学月 **/
	public function getWeekByMonth($semester_id,$week)
	{
		$data = $this->db->fetchRow("SELECT `semester_month` FROM `tbl_week` WHERE week_number='{$week}' AND semester_id ='$semester_id'");
		return $data;
	}

	/** 根据学月得到周数 **/
	public function getWeekCount($semester_id, $semester_month = 0)
	{
		$where = " WHERE semester_id='{$semester_id}' ";
		if (!empty($semester_month)){
			$where .= " AND semester_month='{$semester_month}' ";
		}

		$row = $this->db->fetchAll("SELECT week_number FROM `tbl_week` {$where} GROUP BY week_number");

		return count($row);
	}

	/** 根据学期得到周 **/
	public function getWeekAll($semester_id, $semester_month = 0)
	{

		$where = " WHERE semester_id='{$semester_id}' ";

		if(!empty($semester_month)){
			$where .= " AND semester_month='{$semester_month}' ";
		}

		$lists = $this->db->fetchAll("SELECT week_number FROM `tbl_week` {$where}");

		foreach ($lists as $key => $value){
			$week[]=$value['week_number'];
		}

		return $week;

	}


	/** 得到周开始和结束时间 **/
	public function getWeekInfo($semester_id, $week_number)
	{
		$week_data = $this->db->fetchRow("SELECT * FROM `tbl_week` WHERE semester_id='{$semester_id}' AND week_number='{$week_number}' ");

		return $week_data;

	}
	/** 只读取学年 **/
	public function getTimeAll()
	{
		$semester_lists = $this->db->fetchAll("SELECT min(semester_id),semester_xname FROM `tbl_semester` group by semester_xname ");

		return $semester_lists;
	}
	/** 读取当前学年最大id **/
	public function getTimeRow($time)
	{
		$semester_lists = $this->db->fetchRow("SELECT max(semester_id) FROM `tbl_semester` WHERE create_time LIKE '%{$time}%' ");
		
		return $semester_lists;
	}
	/** 首页（全部第一行） **/
	public function getIndex()
	{
		$semester_data = $this->db->fetchRow("SELECT * FROM `tbl_semester` ");
		
			$semester_data['semester_name'] = $semester_data['semester_xname'].$this->semester_name[$semester_data['semester_name']];
	

		return $semester_data;
	}
	/** 读取一条原声版 **/
	public function getinfoy($semester_id)
	{
		$semester_data = $this->db->fetchRow("SELECT * FROM `tbl_semester` WHERE semester_id='{$semester_id}' ");
		return $semester_data;
	}
	
	
}



?>